// pComboBoxDlg.cpp : implementation file
//
//    Copyright 2008,2009 Paul Jorgensen
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "stdafx.h"
#include "pComboBox.h"
#include "pComboBoxDlg.h"
#include "pf_ParseString2.h"



extern int pi_ErrorLevel;
extern pf_ParseString po_CmdLineTokens;

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };
	//CString ps_ListData[20];  // list data for combobox
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

class CHelpDlg : public CDialog
{
public:
	CHelpDlg();

// Dialog Data
	enum { IDD = IDD_DHELP };
	
	protected:
	//virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	//DECLARE_MESSAGE_MAP()
};

CHelpDlg::CHelpDlg(): CDialog(CHelpDlg::IDD)
{
}


// CpComboBoxDlg dialog




CpComboBoxDlg::CpComboBoxDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CpComboBoxDlg::IDD, pParent)
	, ps_ListData(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CpComboBoxDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_CBString(pDX, IDC_COMBO1, ps_ListData);
	DDV_MaxChars(pDX, ps_ListData, 40);
	
}

BEGIN_MESSAGE_MAP(CpComboBoxDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_COMBO1, &CpComboBoxDlg::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDC_RETURNABORT, &CpComboBoxDlg::OnBnClickedReturnabort)
	ON_BN_CLICKED(IDOK, &CpComboBoxDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BHELP, &CpComboBoxDlg::OnBnClickedBhelp)
END_MESSAGE_MAP()


// CpComboBoxDlg message handlers

BOOL CpComboBoxDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString sTempString3;
	sTempString3 += "inserted";
	CString sMessage1;


	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	// get pointer to app to make updates to window
	//CWinApp* pApp = AfxGetApp();

	// maybe move this to pf_UpdateComboBoxList and use for custom title
	AfxGetMainWnd()->SetWindowTextW(TEXT("WWW.JUSTCODE.CA Paul Jorgensen")); 

	if(po_CmdLineTokens.pf_GetSlashPSet() == TRUE && po_CmdLineTokens.pf_Getpg_SlashPTotalError() == FALSE){
		pf_UpdateComboBoxList();
	}	
	else {
		
		sMessage1 += "slash  [ p ] -not-set or other error";
		AfxMessageBox(sMessage1);
	}
	if(po_CmdLineTokens.pf_GetSlashTSet() == TRUE ){
		
		AfxGetMainWnd()->SetWindowTextW(po_CmdLineTokens.TokenStrings_SlashT[0]); 
	}
	else {
		//sMessage1 += "slash [ t ]-not-set or other error";
		//AfxMessageBox(sMessage1);
	}
	if(po_CmdLineTokens.pf_GetSlashWSet() == TRUE ){

		AfxGetMainWnd()->SetWindowPos(NULL,50,50,350,500,SWP_NOZORDER|SWP_NOMOVE);
	}
	else {
		//sMessage1 += "slash [ w ]-not-set or other error";
		//AfxMessageBox(sMessage1);
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CpComboBoxDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CpComboBoxDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CpComboBoxDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



int CpComboBoxDlg::pf_UpdateComboBoxList(void)
{
	CString sTempString3;
	sTempString3 += "inserted";
	int count;
	CString sMessage1;
	CWnd *pwDialogWind;

	

	pwDialogWind=AfxGetMainWnd();
	
	// use this to allow sizing
	pwDialogWind->SetWindowPos(NULL,0,0,450,85,SWP_NOZORDER|SWP_NOMOVE);
	
	if(po_CmdLineTokens.pf_Getpi_NumTotalTokensFound() != 0 && (po_CmdLineTokens.pf_Getpi_SlashPTotal() != 0)){
	  CDialog::UpdateData(TRUE);

	  CComboBox* pp_ComboBox; // this is a pointer to object of type CComboBox
      pp_ComboBox = (CComboBox*) GetDlgItem(IDC_COMBO1);
	  // iterate through slash p tokens and add to combobox list
	  pp_ComboBox->DeleteString(0);
	  for(count=0;count <= po_CmdLineTokens.pf_Getpi_SlashPTotal();count++){
		//pp_ComboBox->DeleteString(count);
		pp_ComboBox->InsertString(count,po_CmdLineTokens.TokenStrings_SlashP[count] );
	  }
	  pp_ComboBox->SetCurSel(0);
	  return 1;
	}
	else {
		sMessage1 += "pi_SlashPTotal = 0 or TotalTokens =0";
		AfxMessageBox(sMessage1);
		return 0;
	}
}

void CpComboBoxDlg::OnCbnSelchangeCombo1()
{
	// TODO: Add your control notification handler code here
}

void CpComboBoxDlg::OnBnClickedReturnabort()
{
	po_CmdLineTokens.pf_Setpi_ErrorLevel(100);
	EndDialog(1);
	CDialog::CloseWindow();
	
	// *****!!!! also need to quit here.....**********!!!!!!
	// TODO: Add your control notification handler code here
}

void CpComboBoxDlg::OnBnClickedOk()
{
	CString sTempString;
	int result;
	// TODO: Add your control notification handler code here
	// get ptr to combobox, use GetCurSel or something like it
	//to get current item #, set pi_ErrorLevel to item #
	// and other then code cleanup SlashP functionality will be done!
	
	CComboBox* pp_ComboBox; // this is a pointer to object of type CComboBox
    pp_ComboBox = (CComboBox*) GetDlgItem(IDC_COMBO1);
	// have to add one to ErrorLevel - code always starts at 0
	po_CmdLineTokens.pf_Setpi_ErrorLevel(pp_ComboBox->GetCurSel() +1);
	if(pp_ComboBox->GetCurSel() < 0 ){
		GetDlgItemText(IDC_COMBO1,sTempString);
		if(sTempString.IsEmpty() == FALSE){
			//AfxMessageBox(sTempString);
			result=po_CmdLineTokens.pf_WriteResultRegistryItem(sTempString);
			po_CmdLineTokens.pf_Setpi_ErrorLevel(102);
			
	
		}
	}
	// pi_ErrorLevel = 3;
	OnOK();
}

void CpComboBoxDlg::OnBnClickedBhelp()
{
	CHelpDlg dlgHelp;
	dlgHelp.DoModal();
}
